package com.mindtalk;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.provider.ProviderManager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.mindtalk.helper.ConnectionHelper;
import com.mindtalk.screen.MainTab;
import com.mindtalk.transferdata.Transferdata;

public class mindtalk extends Activity implements OnClickListener{
    /** Called when the activity is first created. */

    ChatManager chatmanager;
    XMPPConnection connection;
    Chat newchat;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        Button button = (Button)findViewById(R.id.btn_tes);                
        Button buttonlogout = (Button)findViewById(R.id.btn_logout);        
        buttonlogout.setOnClickListener(this);
        button.setOnClickListener(this);
        
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_tes:{								
			EditText edit_host = (EditText)findViewById(R.id.edit_host);
			EditText edit_port = (EditText)findViewById(R.id.edit_port);
			EditText edit_service = (EditText)findViewById(R.id.edit_service);
			EditText edit_username = (EditText)findViewById(R.id.edit_username);
			EditText edit_password = (EditText)findViewById(R.id.edit_password);
			String host = edit_host.getText().toString();
			String port = edit_port.getText().toString();
			String service = edit_service.getText().toString();
			String username = edit_username.getText().toString();
			String password = edit_password.getText().toString();
			ConnectionConfiguration config;
			if(!service.equals(""))
				config = new ConnectionConfiguration(host,Integer.parseInt(port));
			else
				config = new ConnectionConfiguration(host,Integer.parseInt(port));
			//config.setCompressionEnabled(true);
			config.setSASLAuthenticationEnabled(true);
			ConnectionHelper.configure(ProviderManager.getInstance());
			connection = new XMPPConnection(config);
			//konek ke server
			try{
				connection.connect();
				Log.i("XMPPCLient", "Connected to "+connection.getHost());
			}catch (Exception e) {
				// TODO: handle exception
				Log.e("gagal", "Gagal konek ke: "+connection.getHost()+e.getMessage()+e.getLocalizedMessage());	
			}
			try{				
				connection.login(username, password,"Smack");
				Log.i("connect attemp", "loggedin as" +connection.getUser());
				Presence presence = new Presence(Presence.Type.available);
				connection.sendPacket(presence);
				ConnectionHelper.setConnection(connection);
				Transferdata td = new Transferdata(username, password,host,Integer.parseInt(port),service);
				Intent intent = new Intent(v.getContext(), MainTab.class);
				intent.putExtra("information", td);
				startActivity(intent);																
			}catch (Exception e) {
				// TODO: handle exception
				Log.e("gagal", "gagal konek" + e.getMessage());				
			}
						
			break;
		}		
		default:
			break;
		}
		
	}
}